<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Lottery;
use Carbon\Carbon;

class LotteryController extends Controller
{
    /**
     * 显示摇奖活动列表
     */
    public function index()
    {
        $lotteries = Lottery::latest()->paginate(10);
        return view('lottery.index', compact('lotteries'));
    }

    /**
     * 显示创建摇奖活动的表单
     */
    public function create()
    {
        return view('lottery.create');
    }

    /**
     * 保存新创建的摇奖活动
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'start_time' => 'required|date',
            'end_time' => 'required|date|after:start_time',
            'status' => 'required|in:0,1,2',
        ]);

        Lottery::create($validated);

        return redirect()
            ->route('admin.lottery.index')
            ->with('success', '摇奖活动创建成功！');
    }

    /**
     * 显示指定的摇奖活动
     */
    public function show(Lottery $lottery)
    {
        return view('lottery.show', compact('lottery'));
    }

    /**
     * 显示编辑摇奖活动的表单
     */
    public function edit(Lottery $lottery)
    {
        return view('lottery.create', compact('lottery'));
    }

    /**
     * 更新指定的摇奖活动
     */
    public function update(Request $request, Lottery $lottery)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'start_time' => 'required|date',
            'end_time' => 'required|date|after:start_time',
            'status' => 'required|in:0,1,2',
        ]);

        $lottery->update($validated);

        return redirect()
            ->route('admin.lottery.index')
            ->with('success', '摇奖活动更新成功！');
    }

    /**
     * 删除指定的摇奖活动
     */
    public function destroy(Lottery $lottery)
    {
        $lottery->delete();

        return response()->json([
            'success' => true,
            'message' => '摇奖活动删除成功！'
        ]);
    }

    /**
     * 执行抽奖
     */
    public function draw(Lottery $lottery)
    {
        // 检查活动是否在进行中
        if ($lottery->status !== 1) {
            return response()->json([
                'success' => false,
                'message' => '活动未在进行中！'
            ], 400);
        }

        // 这里添加抽奖逻辑
        // ...

        return response()->json([
            'success' => true,
            'message' => '抽奖成功！',
            'data' => [
                'winners' => [] // 中奖者信息
            ]
        ]);
    }

    /**
     * 显示参与者列表
     */
    public function participants(Lottery $lottery)
    {
        $participants = $lottery->participants()->paginate(15);
        return view('lottery.participants', compact('lottery', 'participants'));
    }

    /**
     * 显示获奖者列表
     */
    public function winners(Lottery $lottery)
    {
        $winners = $lottery->winners()->paginate(15);
        return view('lottery.winners', compact('lottery', 'winners'));
    }
} 